<template>
  <div class="headContainer" id="headContainer">
    <div class="head-left" id="head-left" @click="goBack">
      <i class="iconfont">&#xe603;</i>
    </div>
    <div class="head-right">
      <div class="right_cart" id="right_cart">
        <i class="iconfont"> &#xe63f;</i>
      </div>
      <div class="right_more" id="right_more">
        <i class="iconfont">&#xe61f;</i>
      </div>
    </div>
  </div>
</template>

<script>
import { onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";
export default {
  setup() {
    const router = useRouter();
    const goBack = () => {
      router.push("/qunar/index");
    };
    let temp = null
    // 监听用户滑动屏幕
    function userScroll(e) {
      // console.log('e',e);
      // let scrollTop =
      //   window.pageYOffset ||
      //   document.documentElement.scrollTop ||
      //   document.body.scrollTop;
      let a = document.body.scrollHeight;
      let top =
        document.documentElement.scrollTop ||
        window.pageYOffset ||
        document.body.scrollTop;
      // let top = e.srcElement.scrollingElement.scrollTop;

      if (top > 40) {
        // console.log('滚动高度',top)
        // document.getElementByClassName("headContainer").style.backgroundColor = "#00BCD4"
        document.getElementById("headContainer").style.backgroundColor = "#00BCD4";
        document.getElementById("right_cart").style.backgroundColor = "#00BCD4";
        document.getElementById("right_more").style.backgroundColor = "#00BCD4";
        document.getElementById("head-left").style.backgroundColor = "#00BCD4";

        document.getElementById("right_more").style.color = "white";
        document.getElementById("right_cart").style.color = "white";
        document.getElementById("head-left").style.color = "white";
      } else {
        document.getElementById("headContainer").style.backgroundColor = "";
        document.getElementById("right_cart").style.backgroundColor =
          "rgba(0, 0, 0, 0.5)";
        document.getElementById("right_more").style.backgroundColor =
          "rgba(0, 0, 0, 0.5)";
        document.getElementById("head-left").style.backgroundColor = "rgba(0, 0, 0, 0.5)";

        document.getElementById("right_more").style.color = "white";
        document.getElementById("right_cart").style.color = "white";
        document.getElementById("head-left").style.color = "white";
      }
    }
    // 节流函数
    function throttle(func, delay = 500) {
      let timer = null;
      const context = this;
      return function (...args) {
        // 如果定时器不存在
        if (!timer) {
          timer = setTimeout(() => {
            func.apply(context, args); // 考虑返回的函数调用的环境，因此这里不直接使用this
            timer = null; // delay之后清除定时器
          }, delay);
        }
      };
    }
    onMounted(() => {
       temp = throttle(userScroll, 500)
      document.addEventListener("scroll", ()=>{
        temp()
      });
    });
    onUnmounted(() => {
      // console.log("组件卸载");
      clearTimeout()
      document.removeEventListener("scroll", temp);
    });
    return {
      goBack,
    };
  },
};
</script>

<style scoped>
.headContainer {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 50px;
  display: flex;
  justify-content: space-between;
  /* font-size: 20px; */
  padding: 8px 10px;
  box-sizing: border-box;
  z-index: 99999;
}
.headContainer .iconfont {
  font-size: 25px;
  color: white;
}
.head-right {
  display: flex;
}
.head-left,
.right_cart,
.right_more {
  width: 35px;
  height: 35px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  /* align-content: center; */
  align-items: center;
  /* opacity: 0.7; */
}
.right_cart {
  margin-right: 8px;
}
</style>
